/*
 * view_avi.cpp
 *
 *  Created on: May 27, 2009
 *      Author: andrei
 */

#include <cv.h>
#include <highgui.h>
#include <stdio.h>
//int main( int argc, char** argv ) {
void view_movie()
{
	//cvNamedWindow("Haar Window", CV_WINDOW_AUTOSIZE);

	cvNamedWindow("Example2",CV_WINDOW_AUTOSIZE);
	CvCapture* capture;
	capture = cvCreateCameraCapture(0);
	// Rest of program proceeds totally ignorant
	//CvCapture* capture = cvCreateFileCapture("tree.avi");
    IplImage* frame;

    /*
    CvHaarClassifierCascade * pCascade = 0;  // the face detector
    CvMemStorage * pStorage = 0;        // expandable memory buffer
    CvSeq * pFaceRectSeq;               // list of detected faces

    pStorage = cvCreateMemStorage(0);
    pCascade = (CvHaarClassifierCascade *)cvLoad("trainout.xml",0, 0, 0 );
    */
    int k=7;
    char * filename;
    while(1) {
        frame = cvQueryFrame( capture );
        if( !frame ) break;
        cvShowImage("Example2" , frame);
														//char * filename;
														//sprintf(filename, "test%d.jpg",j);

        // declarations
        //CvHaarClassifierCascade * pCascade = 0;  // the face detector
        //CvMemStorage * pStorage = 0;        // expandable memory buffer
        //CvSeq * pFaceRectSeq;               // list of detected faces
        //int i;

        // initializations					//hand_gestures-002.jpg
        //IplImage* pInpImg = cvLoadImage("test4.jpg", CV_LOAD_IMAGE_COLOR);//CoupeDavis.jpg
        //pStorage = cvCreateMemStorage(0);
        //pCascade = (CvHaarClassifierCascade *)cvLoad("trainout.xml",0, 0, 0 );
        											//haarcascade_frontalface_default.xml
        												//cascade.xml
        // validate that everything initialized properly
        /*
        if( !frame || !pStorage || !pCascade )
        {
        	printf("Initialization failed: %s \n",(!frame)?  "didn't load image file" :(!pCascade)? "didn't load Haar cascade -- "
                        "make sure path is correct" :
        		"failed to allocate memory for data storage");
        		exit(-1);
        }

       	// detect faces in image
       	pFaceRectSeq = cvHaarDetectObjects
       		(frame, pCascade, pStorage,
        		1.05,                       // increase search scale by 10% each pass
        		3,                         // drop groups of fewer than three detections
        		CV_HAAR_DO_CANNY_PRUNING,  // skip regions unlikely to contain a face
        		cvSize(0,0));              // use XML default for smallest search scale

       	// create a window to display detected faces
       	//cvNamedWindow("Haar Window", CV_WINDOW_AUTOSIZE);

        	// draw a rectangular outline around each detection
        	for(i=0;i<(pFaceRectSeq? pFaceRectSeq->total:0); i++ )
        	{
        		CvRect * r = (CvRect*)cvGetSeqElem(pFaceRectSeq, i);
        		CvPoint pt1 = { r->x, r->y };
        		CvPoint pt2 = { r->x + r->width, r->y + r->height };
        		cvRectangle(frame, pt1, pt2, CV_RGB(0,255,0), 3, 4, 0);
        	}

        	// display face detections
        	//cvShowImage("Haar Window", frame);
        	//cvWaitKey(0);
        	//cvDestroyWindow("Haar Window");

        	// clean up and release resources
        	//cvReleaseImage(&pInpImg);
          	//if(pCascade) cvReleaseHaarClassifierCascade(&pCascade);
            //if(pStorage) cvReleaseMemStorage(&pStorage);
        	//return 0;
		*/
    	cvSaveImage("foto_test2.jpg",frame);
//
        //ku
        //cvSaveImage("imag7.jpg",frame);
        //j++;
        //Once we have displayed the frame, we then wait for 33 ms
        char c = cvWaitKey(33);

        //If the user hits the Esc key (ASCII 27), then we will exit the read loop
        /*
        if (c== 27)
        {
        	//char * filename;
        	fprintf("here %d",k);
        	filename=" ";
        	sprintf(filename,"imag%d.jpg",k);
        	k++;
        }
		*/
        if ( c == 27 ) break;
    }
    //cvReleaseHaarClassifierCascade(&pCascade);
    //cvReleaseMemStorage(&pStorage);
    cvReleaseCapture( &capture );
    cvDestroyWindow( "Example2");
}
